/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function (matrix, target) {
  if (matrix.length === 0 || matrix[0].length === 0) return false

  let m = matrix.length
  let n = matrix[0].length

  //创建一个一位数组
  let arr = new Array(m * n).fill(0)
  let pos = 0
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      arr[pos++] = matrix[i][j]
    }
  }

  //二分法查找target
  let left = 0, right = arr.length - 1
  while (left <= right) {
    let mid = Math.floor((left + right) / 2)
    if (arr[mid] === target) {
      return true
    } else if (arr[mid] > target) {
      right = mid - 1
    } else {
      left = mid + 1
    }
  }

  return false
};